package com.yumi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yumi.common.Result;
import com.yumi.entity.Task;
import com.yumi.service.employeeTestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Tag(name = "任务管理", description = "任务的增删改查接口")
@RestController
@Slf4j
@RequestMapping("api/tasks")
@RequiredArgsConstructor
public class EmployeeTestController {

    private final employeeTestService TaskService;

    /**
     * 新增任务
     * @param task 任务信息
     * @return 操作结果
     */
    @Operation(summary = "新增任务", description = "创建一个新的任务")
    @ApiResponse(responseCode = "200", description = "创建成功")
    @PostMapping
    public Result InsertTask(@RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "任务信息", 
            required = true, 
            content = @Content(schema = @Schema(implementation = Task.class))
        ) Task task){
        log.info("开始新增任务{}",task);
        TaskService.save(task);
        return Result.success();
    }

    /**
     * 分页查询任务
     * @param page 页码
     * @param pageSize 每页数量
     * @param title 任务标题（可选）
     * @param status 任务状态（可选）
     * @return 分页结果
     */
    @Operation(summary = "分页查询任务", description = "根据条件分页查询任务列表")
    @ApiResponse(responseCode = "200", description = "查询成功", 
                 content = @Content(schema = @Schema(implementation = Page.class)))
    @GetMapping("/pages")
    public Result<Page<Task>> page(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "任务标题（模糊查询）") @RequestParam(required = false) String title,
            @Parameter(description = "任务状态：0-未开始 1-进行中 2-已完成") @RequestParam(required = false) Integer status) {
        
        log.info("接收分页查询任务请求");
        Page<Task> result = TaskService.pageQuery(page, pageSize, title, status);
        return Result.success(result);
    }

    /**
     * 根据ID查询任务详情
     * @param id 任务ID
     * @return 任务详情
     */
    @Operation(summary = "查询任务详情", description = "根据ID查询任务详情")
    @ApiResponse(responseCode = "200", description = "查询成功",
                 content = @Content(schema = @Schema(implementation = Task.class)))
    @GetMapping("/{id}")
    public Result<Task> getTaskById(@PathVariable Integer id) {
        log.info("开始获取任务详情，任务ID：{}", id);
        Task task = TaskService.getById(id);
        return Result.success(task);
    }

    /**
     * 更新任务
     * @param task 任务对象(需包含ID)
     * @return 操作结果
     */
    @Operation(summary = "更新任务", description = "根据ID更新任务信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping
    public Result updateTask(@RequestBody Task task) {
        log.info("开始更新任务，任务ID：{}", task.getId());
        TaskService.updateById(task);
        return Result.success();
    }

    /**
     * 根据ID删除任务
     * @param id 任务ID
     * @return 操作结果
     */
    @Operation(summary = "删除任务", description = "根据ID删除任务信息")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result deleteTask(@PathVariable Integer id) {
        log.info("开始删除任务，任务ID：{}", id);
        TaskService.removeById(id);
        return Result.success();
    }
}